<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		Broadcastweb.eu - Paules												/
/	Site:			http://www.broadcastweb.eu												/
/	Email:			support@broadcastweb.eu													/
/	Copyright:		2011 - 2012 (c) Broadcastweb.eu											/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0003																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla security
defined('_JEXEC') or die;
jimport('joomla.application.component.controllerform');
jimport('joomla.database.table');
 
// Controller Server view
class JooSamControllerServer extends JControllerForm{
	protected $text_prefix = 'Server';

	public function save($key = null, $urlVar = null){

		$data = JRequest::getVar('jform', array(), 'post', 'array');
		$error = 0;
		//New String
		if ($data['id'] == '0'){
			$addServer = $this->addServer();
			if($addServer){
				$txt = '<table><tr><td>'.JText::_('COM_JOOSAM_SERVER_CONT_SERVERADDED').'</td></tr>';
				$strings = $this->addTxTcodes();
				if($strings){$txt .= '<tr><td>'.JText::_('COM_JOOSAM_SERVER_CONT_DEFAULTTXTADDED').'</td></tr>';}
				else{$txt .= '<tr><td>'.JText::_('COM_JOOSAM_SERVER_CONT_ERROR_ADDTXT').'</td></tr>'; $error = 1;}
				$event = $this->addDefEvent();
				if($event){$txt .= '<tr><td>'.JText::_('COM_JOOSAM_SERVER_CONT_REQEVTCONG_CREATED').'</td></tr>';}
				else{$txt .= '<tr><td>'.JText::_('COM_JOOSAM_SERVER_CONT_ERROR_REQEVTCONG_CREATED').'</td></tr>'; $error = 1;}
				$txt .= '</table>';
				if($error == 1){ $this->setMessage($txt, 'warning'); }
				else{$this->setMessage($txt);}
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=servers', false));
			}
		}
		else{
			$editServer = $this->editServer();
			if($editServer){
				$txt = JText::_('Edit done');
				$this->setMessage($txt);
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=servers', false));
			}
		}
	}

	protected function addServer(){

		$data = JRequest::getVar('jform', array(), 'post', 'array');

		$sql_ip = $data['sql_ip'];
		$sql_port = $data['sql_port'];
		$sql_user = $data['sql_user'];
		$sql_pass = $data['sql_pass'];
		if($sql_pass != ''){ $sql_pass = JoosamHelper::jsmCrypt('encrypt', $sql_pass); }
		$sql_db = $data['sql_db'];
		$srv_name = $data['srv_name'];
		$srv_descr = $data['srv_descr'];
		$srv_active = 0;
		$set_admin = 0;
		$sam_ip = $data['sam_ip'];
		$sam_port = $data['sam_port'];
		$sam_timezone = $data['sam_timezone'];
		$image_type = $data['image_type'];
		$image_data = $data['image_data'];

		$db =& JFactory::getDBO();
		$query = "	INSERT INTO #__joosam_server (sql_ip,sql_port,sql_user,sql_pass,sql_db,srv_name,srv_descr,srv_active,set_admin,sam_ip,sam_port,sam_timezone,image_type,image_data)
					VALUES ('".$sql_ip."','".$sql_port."','".$sql_user."','".$sql_pass."','".$sql_db."','".$srv_name."','".$srv_descr."','".$srv_active."','".$set_admin."','".$sam_ip."','".$sam_port."','".$sam_timezone."','".$image_type."','".$image_data."')";
		$db->setQuery($query);
		$build = $db->query();

		if($build){
			return true;
		}
		else{
			return false;
		}
	}

	protected function editServer(){

		$data = JRequest::getVar('jform', array(), 'post', 'array');

		$sql_ip = $data['sql_ip'];
		$sql_port = $data['sql_port'];
		$sql_user = $data['sql_user'];
		$sql_pass = $data['sql_pass'];
		if($sql_pass != ''){ $sql_pass = JoosamHelper::jsmCrypt('encrypt', $sql_pass); }
		$sql_db = $data['sql_db'];
		$srv_name = $data['srv_name'];
		$srv_descr = $data['srv_descr'];
		$sam_ip = $data['sam_ip'];
		$sam_port = $data['sam_port'];
		$sam_timezone = $data['sam_timezone'];
		$image_type = $data['image_type'];
		$image_data = $data['image_data'];

		$db =& JFactory::getDBO();
		if($sql_pass != ''){
			$query = "	UPDATE ".$db->nameQuote('#__joosam_server')."
							SET ".$db->nameQuote('sql_ip')." = ".$db->quote($sql_ip).",
								".$db->nameQuote('sql_port')." = ".$db->quote($sql_port).",
								".$db->nameQuote('sql_user')." = ".$db->quote($sql_user).",
								".$db->nameQuote('sql_pass')." = ".$db->quote($sql_pass).",
								".$db->nameQuote('sql_db')." = ".$db->quote($sql_db).",
								".$db->nameQuote('srv_name')." = ".$db->quote($srv_name).",
								".$db->nameQuote('srv_descr')." = ".$db->quote($srv_descr).",
								".$db->nameQuote('image_type')." = ".$db->quote($image_type).",
								".$db->nameQuote('image_data')." = ".$db->quote($image_data).",
								".$db->nameQuote('sam_ip')." = ".$db->quote($sam_ip).",
								".$db->nameQuote('sam_port')." = ".$db->quote($sam_port).",
								".$db->nameQuote('sam_timezone')." = ".$db->quote($sam_timezone)."
							WHERE ".$db->nameQuote('id')." = ".$db->quote($data['id']).";";
		}
		else{
			$query = "	UPDATE ".$db->nameQuote('#__joosam_server')."
							SET ".$db->nameQuote('sql_ip')." = ".$db->quote($sql_ip).",
								".$db->nameQuote('sql_port')." = ".$db->quote($sql_port).",
								".$db->nameQuote('sql_user')." = ".$db->quote($sql_user).",
								".$db->nameQuote('sql_db')." = ".$db->quote($sql_db).",
								".$db->nameQuote('srv_name')." = ".$db->quote($srv_name).",
								".$db->nameQuote('srv_descr')." = ".$db->quote($srv_descr).",
								".$db->nameQuote('image_type')." = ".$db->quote($image_type).",
								".$db->nameQuote('image_data')." = ".$db->quote($image_data).",
								".$db->nameQuote('sam_ip')." = ".$db->quote($sam_ip).",
								".$db->nameQuote('sam_port')." = ".$db->quote($sam_port).",
								".$db->nameQuote('sam_timezone')." = ".$db->quote($sam_timezone)."
							WHERE ".$db->nameQuote('id')." = ".$db->quote($data['id']).";";
		}
		$db->setQuery($query);
		$build = $db->query();

		if($build){
			return true;
		}
		else{
			return false;
		}
	}

	// Add Default text codes
	protected function addTxTcodes(){

		$data = JRequest::getVar('jform', array(), 'post', 'array');

		$db1 =& JFactory::getDBO();
		$query1 = "
					SELECT ".$db1->nameQuote('id')."
					FROM ".$db1->nameQuote('#__joosam_server')."
					ORDER BY ".$db1->nameQuote('id')." DESC
				";
		$db1->setQuery($query1);
		$srvID = $db1->loadResult();

		if($srvID){
			$db2 =& JFactory::getDBO();
				$query2 = "
					INSERT INTO #__joosam_textstrings
							(srv_id, txt_code, txt_string)
					VALUES 
							('".$srvID."', '200', 'COM_JOOSAM_RESPONSE_TXT_200'),
							('".$srvID."', '600', 'COM_JOOSAM_RESPONSE_TXT_500'),
							('".$srvID."', '600', 'COM_JOOSAM_RESPONSE_TXT_501'),
							('".$srvID."', '600', 'COM_JOOSAM_RESPONSE_TXT_502'),
							('".$srvID."', '600', 'COM_JOOSAM_RESPONSE_TXT_503'),
							('".$srvID."', '600', 'COM_JOOSAM_RESPONSE_TXT_600'),
							('".$srvID."', '601', 'COM_JOOSAM_RESPONSE_TXT_601'),
							('".$srvID."', '602', 'COM_JOOSAM_RESPONSE_TXT_602'),
							('".$srvID."', '603', 'COM_JOOSAM_RESPONSE_TXT_603'),
							('".$srvID."', '604', 'COM_JOOSAM_RESPONSE_TXT_604'),
							('".$srvID."', '605', 'COM_JOOSAM_RESPONSE_TXT_605'),
							('".$srvID."', '606', 'COM_JOOSAM_RESPONSE_TXT_606'),
							('".$srvID."', '609', 'COM_JOOSAM_RESPONSE_TXT_609'),
							('".$srvID."', '609', 'COM_JOOSAM_RESPONSE_TXT_610'),
							('".$srvID."', '700', 'COM_JOOSAM_RESPONSE_TXT_700'),
							('".$srvID."', '701', 'COM_JOOSAM_RESPONSE_TXT_701'),
							('".$srvID."', '702', 'COM_JOOSAM_RESPONSE_TXT_702'),
							('".$srvID."', '703', 'COM_JOOSAM_RESPONSE_TXT_703'),
							('".$srvID."', '704', 'COM_JOOSAM_RESPONSE_TXT_704'),
							('".$srvID."', '705', 'COM_JOOSAM_RESPONSE_TXT_705'),
							('".$srvID."', '706', 'COM_JOOSAM_RESPONSE_TXT_706'),
							('".$srvID."', '707', 'COM_JOOSAM_RESPONSE_TXT_707'),
							('".$srvID."', '708', 'COM_JOOSAM_RESPONSE_TXT_708');
						";
			$db2->setQuery($query2);
			$txtdata = $db2->query();

			if($txtdata){
				return true;
			}
			else{
				return false;
			}
		}
		else{
			return false;
		}
	}

	// Add Default Event
	protected function addDefEvent(){

		$data = JRequest::getVar('jform', array(), 'post', 'array');

		$db1 =& JFactory::getDBO();
		$query1 = "
					SELECT ".$db1->nameQuote('id')."
					FROM ".$db1->nameQuote('#__joosam_server')."
					ORDER BY ".$db1->nameQuote('id')." DESC
				";
		$db1->setQuery($query1);
		$srvID = $db1->loadResult();

		if($srvID){
			$db2 =& JFactory::getDBO();
				$query2 = "
					INSERT INTO #__joosam_evtreq
							(srv_id, evt_time_start, evt_time_end, evt_output_title, evt_output_content, evt_name, evt_description, auto_update, front_req_text)
					VALUES 
							('".$srvID."', '00:00:00', '00:00:00', 'Default', 'Default Event', 'Default', 'Default Event', '2', '<h2>JooSam Request</h2><p>Change me at your joomla adminpage</p>')";
			$db2->setQuery($query2);
			$evtdata = $db2->query();

			if($evtdata){
				return true;
			}
			else{
				return false;
			}
		}
		else{
			return false;
		}
	}
}